$(document).ready(function () {
    var jcrop_api, boundx, boundy,
    _scrollWidth = Math.max(document.body.scrollWidth,document.documentElement.clientWidth),
    _scrollHeight = Math.max(document.body.scrollHeight,document.documentElement.clientHeight);

    $("#fil").bind('select',function(){
        alert("1");
        var nf = obj.cloneNode(true);
        nf.value=''; // 设计新控件value为
        obj.parentNode.replaceChild(nf, obj);
    });
   $("#fil").change(eventChange);
   function eventChange(){
           clearCoords();
           $("#frm2").ajaxSubmit({
               dataType:'json',
               success:function(msg){
                   $('#fil').replaceWith('<input id="fil" class="fil" type="file" name="file" size="1">');
                   $("#fil").change(eventChange);
                   //var msg= eval('(' + msg + ')');
                       if(msg.code){

                           $("#shade").css("width",_scrollWidth+"px");
                           $("#shade").css("height",_scrollHeight+"px");
                           $("#shade").css("display","block");
                           //文件上传成功后执行,msg为服务器端返回的信息
                           $("#dielog").css("display","block");
                           if(parseInt(msg.height) > 320){
                              $("#dielog").css("height",msg.height);
                           }else{
                              $("#dielog").css("height",320);
                           }
                           $("#jcorp").html('<img width="320px" height="'+msg.height+'px" id="jc-image" name="img" src="'+msg.url+'"/>');
                           $("#smldiolog").html('<img id="preview" src="'+msg.url+'"/>');

                           $('#jc-image').Jcrop({
                               onChange:showCoords,
                               onSelect:showCoords,
                               onRelease:clearCoords,
                               aspectRatio: 1
                           },function(){
                               var bounds = this.getBounds();

                               boundx = bounds[0];
                               boundy = bounds[1];
                               jcrop_api = this;
                               var bH = (document.body.clientWidth-500)/2;
                               $("#dielog").css("left",bH+"px");
                           });

                           //                    $('#fil').replaceWith('<input id="fil" class="fil" type="file" name="file" size="1">');
                       }else{
                           alert(msg.msg);
                       }
               },
		       error: function(){
                   alert("Sorry，文件超出了规定的大小，请确保您上传的文件不超过2M！");
		       }
           });


   }
    function showCoords(c)
    {
        $('#x1').val(c.x);
        $('#y1').val(c.y);
        $('#x2').val(c.x2);
        $('#y2').val(c.y2);
        $('#w').val(c.w);
        $('#h').val(c.h);
        if (parseInt(c.w) > 0)
        {
            var rx = 100 / c.w;
            var ry = 100 / c.h;

            $('#preview').css({
                width: Math.round(rx * boundx) + 'px',
                height: Math.round(ry * boundy) + 'px',
                marginLeft: '-' + Math.round(rx * c.x) + 'px',
                marginTop: '-' + Math.round(ry * c.y) + 'px'
            });
        }
    }

    function clearCoords()
    {
        $('#dd1 input').val('');
        $("#smldiolog").val('');
    }
    $("#diocla").click(function(){
        clearCoords();
        $("#dielog").css("display","none");
        $("#shade").css("display","none");
        var old_img=$('#jc-image').attr("src");
        jQuery.ajax({
            url:viewPositionUrl,
            data:{'old_img':old_img,'code':'cancel'},
            type:'post'
        });
    });

    $("#sub1").click(function(){
        var old_img=$('#jc-image').attr("src"),
            x1=$("#x1").val(),
            y1=$("#y1").val(),
            x2=$("#x2").val(),
            y2=$("#y2").val(),
            w=$("#w").val(),
            h=$("#h").val();
        clearCoords();
        $("#dielog").css("display","none");
        $("#shade").css("display","none");
        jQuery.ajax({
            url:viewPositionUrl,
            data:{'img':old_img,'x1':x1,'y1':y1,'x2':x2,'y2':y2,'w':w,'h':h},
            type:'post',
            success:function (result) {
                var data = eval('(' + result + ')');
                var num = Math.random();
                if(data.code){
                    $('#ig1').attr("src",data.url+'?'+num);
                }

            }
        });
    });

});
//编辑
function edit(){
    $('#a_edit').hide();
    $('#edit_show').show();
    $('.show').each(function(){
        $(this).attr('class','hide1');
    });
    $('.hide').each(function(){
    	$(this).attr('class','show');
    });
    $('.hide1').each(function(){
        $(this).attr('class','hide');
    });
}
//取消
function cancel(){
    $('#a_edit').show();
    $('#edit_show').hide();
    $('.show').each(function(){
        $(this).attr('class','hide1');
    });
    $('.hide').each(function(){
    	$(this).attr('class','show');
    });
    $('.hide1').each(function(){
        $(this).attr('class','hide');
    });
}
